package com.bluefox.hospital.controller;

import com.bluefox.common.web.dto.result.PageResultDTO;
import com.bluefox.common.web.dto.result.ResultDTO;
import com.bluefox.hospital.convertor.ImageConvertor;
import com.bluefox.hospital.dto.ImageDTO;
import com.bluefox.hospital.model.Image;
import com.bluefox.hospital.repository.ImageRepository;
import com.bluefox.hospital.service.ImageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Image的管理接口
 *
 * @author
 */
@Slf4j
@RestController
@RequestMapping("/w/images")
public class ImageController {
    @Autowired
    private ImageService imageService;
    @Autowired
    private ImageRepository imageRepository;
    @Autowired
    private ImageConvertor imageConvertor;

    /**
     * 获取分页数据
     *
     * @param pageable 分页+排序参数
     * @return 分页数据
     */
    @RequestMapping(value = "/s", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public PageResultDTO<ImageDTO> search(@RequestParam("type") String type, final Pageable pageable) {
        final Page<Image> models = this.imageRepository.findByTypeOrderByAutoOrder(type, pageable);
        return this.imageConvertor.toResultDTO(models);
    }

    /**
     * 取得详细数据
     *
     * @param id 资源ID
     * @return 资源详细
     */

    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<ImageDTO> get(@PathVariable final Long id) {
        final Image model = this.imageService.get(id);
        return this.imageConvertor.toResultDTO(model);
    }

    /**
     * 新建操作
     *
     * @param imageDTO 新建资源的DTO
     * @return 新建资源
     */

    @RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<ImageDTO> create(@RequestBody @Valid final ImageDTO imageDTO) {
        final Image model = this.imageConvertor.toModel(imageDTO);
        this.imageService.create(model);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was created.", Image.class.getSimpleName(), model.getId());
        }
        return this.imageConvertor.toResultDTO(model);
    }

    /**
     * 更新操作
     *
     * @param id       更新资源的ID
     * @param imageDTO 更新资源的DTO
     * @return 更新后资源
     */

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<ImageDTO> update(@PathVariable final Long id, @RequestBody @Valid final ImageDTO imageDTO) {
        imageDTO.setId(id);
        final Image model = this.imageConvertor.toModel(imageDTO);
        this.imageService.update(model);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was updated.", Image.class.getSimpleName(), model.getId());
        }
        return this.imageConvertor.toResultDTO(model);
    }

    /**
     * 删除操作
     *
     * @param Id 资源ID
     * @return 操作结果
     */

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.imageService.delete(id);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was deleted.", Image.class.getSimpleName(), id);
        }
        return ResultDTO.success();
    }

    @RequestMapping(value = "/getImageTop5", method = RequestMethod.GET)
    public List<ImageDTO> getTopThree() {
        List<Image> top5ByTypeOne = this.imageService.getImageTop10ByTypeOne();
        return this.imageConvertor.toListDTO(top5ByTypeOne);
    }

    @RequestMapping(value = "/getMidImg", method = RequestMethod.GET)
    public List<ImageDTO> getMidImg() {
        List<Image> top1ByTypeOne = this.imageService.getMidImg();
        return this.imageConvertor.toListDTO(top1ByTypeOne);
    }

    /**
     * 新建医院环境图片
     *
     * @param imageDTO 新建资源的DTO
     * @return 新建资源
     */

    @RequestMapping(value = "/createEnvirImg", method = RequestMethod.POST)
    public ResultDTO<ImageDTO> createEnvirImg(@RequestBody @Valid final ImageDTO imageDTO) {
        if (imageDTO.getAutoOrder() == null) {
            int order = this.imageService.findMaxOrder();
            imageDTO.setAutoOrder(order);
        }
        final Image model = this.imageConvertor.toModel(imageDTO);
        this.imageService.create(model);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was created.", Image.class.getSimpleName(), model.getId());
        }
        return this.imageConvertor.toResultDTO(model);
    }

    @RequestMapping(value = "/createCarousel", method = RequestMethod.POST)
    public ResultDTO<ImageDTO> createCarousel(@RequestBody @Valid final ImageDTO imageDTO) {
        if (imageDTO.getAutoOrder() == null) {
            int order = this.imageService.findMaxOrderByTypeOne();
            imageDTO.setAutoOrder(order);
        }
        final Image model = this.imageConvertor.toModel(imageDTO);
        this.imageService.create(model);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was created.", Image.class.getSimpleName(), model.getId());
        }
        return this.imageConvertor.toResultDTO(model);
    }

    /**
     * 更新操作
     *
     * @param imageDTO 更新资源的DTO
     * @return 更新后资源
     */
    @RequestMapping(value = "/upAndDown/{flag}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<ImageDTO> upAndDown(@PathVariable final String flag, @RequestBody @Valid final ImageDTO imageDTO) {
        Image model = new Image();
        Image other = new Image();
        if ("up".equals(flag)) {
            other = imageRepository.findByTypeAndAutoOrder(imageDTO.getType(), imageDTO.getAutoOrder() - 1);
            other.setAutoOrder(other.getAutoOrder() + 1);
            imageDTO.setAutoOrder(imageDTO.getAutoOrder() - 1);
            model = this.imageConvertor.toModel(imageDTO);
        } else {
            other = imageRepository.findByTypeAndAutoOrder(imageDTO.getType(), imageDTO.getAutoOrder() + 1);
            other.setAutoOrder(other.getAutoOrder() - 1);
            imageDTO.setAutoOrder(imageDTO.getAutoOrder() + 1);
            model = this.imageConvertor.toModel(imageDTO);
        }
        this.imageService.update(model);
        this.imageService.update(other);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was updated.", Image.class.getSimpleName(), model.getId());
        }
        return this.imageConvertor.toResultDTO(model);
    }
}
